package com.ucomm.talent.widget;

import com.ucomm.talent.AppConstants;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.JobOffers;
import com.ucomm.talent.activity.ResumeActivity;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.view.View;

/**
 * 
 * @author 
 * @Description:搜索栏
 * @date:2013-7-3
 *
 */
public class SearchBarView extends LinearLayout implements View.OnClickListener{

	private View view;
	private Spinner spinner;
	private EditText editText;
	private Button bt_search;
	private Context context;

	public SearchBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		//获取inflate
		LayoutInflater inflater = LayoutInflater.from(context);
		//填充View
		view = inflater.inflate(R.layout.search_layout, null);
		spinner = (Spinner) view.findViewById(R.id.spinner_selecter);
		editText = (EditText) view.findViewById(R.id.et_search);
		editText.clearFocus();
		bt_search = (Button) view.findViewById(R.id.bt_search); //ArrayAdapter<String>(this,R.layout.simple_spinner_item);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.simple_spinner_item);
        String level[] = getResources().getStringArray(R.array.search_content_list);//资源文件
        for (int i = 0; i < level.length; i++) {
            adapter.add(level[i]);
        }
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        bt_search.setOnClickListener(this);
        this.addView(view);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
        int id = v.getId();
        String url = null;
        String text = null;
        text = editText.getText().toString();
        if (id == bt_search.getId()) {       
        	if(text!=null){
        		if(text.equals("")){
        			Toast.makeText(context, "输入不能为空", Toast.LENGTH_SHORT).show();
        		}else{
        			//获取查找类型
        			if(spinner.getSelectedItem().toString().equals("找职位")){
        				//拼接url
        				url = AppConstants.JOB_SEARCH+text;
        				Intent i = new Intent();
        				i.putExtra("url", url);
        				i.setClass(context, JobOffers.class);
        				context.startActivity(i);
        				
        			}else if (spinner.getSelectedItem().toString().equals("找人才")){
        				url = AppConstants.RESUME_SEARCH+text;
        				Intent i = new Intent();
        				i.putExtra("url", url);
        				i.setClass(context, ResumeActivity.class);
        				context.startActivity(i);
        			}
        		}
        	}
        }
		
	}
	

}
